﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Domain.Models;
using Utilities;

namespace Domain.DAL
{
    public class RolesOfUsersDAO
    {
        #region Private Fields

        private Database db;

        #endregion

        #region Constructors

        public RolesOfUsersDAO(Database theDatabase)
        {
            this.db = theDatabase;
        }

        #endregion

        #region Public Methods

        public DataSet GetByUser(int userID)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = this.db.ExecuteDataSet("sp_RolesOfUsers_GetByUser", userID);
            }
            catch (Exception)
            {
                ds.Dispose();
                throw;
            }

            return ds;
        }

        public bool DeleteByUser(string userID)
        {
            bool result = false;

            try
            {
                result = this.db.ExecuteScalar("sp_RolesOfUsers_DeleteByUser", userID).ToString() == userID;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        public int Insert(RolesOfUser theRolesOfUser)
        {
            int result = MyConfig.NullNumber;
    
            try
            {
                result = Convert.ToInt32(this.db.ExecuteScalar("sp_RolesOfUsers_Insert", theRolesOfUser.UserID, theRolesOfUser.RoleID));
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }

        #endregion

        #region Public Static Methods

        public static RolesOfUsersDAO GetInstance()
        {
            return EnterpriseLibraryContainer.Current.GetInstance<RolesOfUsersDAO>();
        }

        #endregion
    }
}
